import Editor from "../editor"
import { Operation } from "./baseOperation"
import { InsertTextOperation } from "./insertTextOperation"
import SetSelectionOperation from "./setSelectionOperation"

export class RemoveTextOperation extends Operation {
  public type: string = "RemoveTextOperation"
  private spacers_: string = ""
  constructor(private startIndex_: number, private endIndex_: number) {
    super()
  }

  apply(editor: Editor) {
    this.spacers_ =
      editor
        .getTextModel()
        ?.getSpacersByIndex(this.startIndex_, this.endIndex_) || ""
    editor.getTextModel()?.remove(this.startIndex_, this.endIndex_)
  }

  reverse(): Operation[] {
    return [
      new InsertTextOperation(this.spacers_, this.startIndex_),
      new SetSelectionOperation({
        anchor:
          this.spacers_.length === 1
            ? this.startIndex_ + this.spacers_.length
            : this.startIndex_,
        focus: this.startIndex_ + this.spacers_.length,
      }),
    ]
  }
}
export default RemoveTextOperation
